﻿using Microsoft.EntityFrameworkCore;
using PhotoGallery.Models;
using System;
using System.Collections.Generic;


namespace PhotoGallery.Repositories
{
    public class PhotoGalleryRepository : DbContext
    {
        public PhotoGalleryRepository(DbContextOptions<PhotoGalleryRepository> options) : base(options)
        {
            
        }

        /// <summary>
        /// 用户
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// 照片
        /// </summary>
        public DbSet<PhotoInfoModel> Photos { get; set; }

        /// <summary>
        /// 相册
        /// </summary>
        public DbSet<AlbumInfoModel> Albums { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=photoGallery.db");
#if DEBUG
            optionsBuilder.EnableSensitiveDataLogging(true);
#endif
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasKey(m => m.UId);
            modelBuilder.Entity<User>().Property(m => m.UserName)
                .IsRequired(true)
                .HasMaxLength(32);
            modelBuilder.Entity<User>().Property(m => m.LastLoginTime)
                .HasColumnType("datetime");
            modelBuilder.Entity<User>().Property(m => m.CreateDate)
                .HasColumnType("datetime");

            modelBuilder.Entity<PhotoInfoModel>().HasIndex(m => m.AlbumName);//设置索引
            modelBuilder.Entity<PhotoInfoModel>().Property(m => m.AlbumName)
                .IsRequired(true);
            modelBuilder.Entity<PhotoInfoModel>().Property(m => m.Name)
                .IsRequired(true);
            modelBuilder.Entity<PhotoInfoModel>().Property(m => m.UploadDate)
                .HasColumnType("datetime");

            modelBuilder.Entity<AlbumInfoModel>().HasKey(w => w.Id);
            modelBuilder.Entity<AlbumInfoModel>().HasIndex(m => m.AlbumPath);//设置索引
            modelBuilder.Entity<AlbumInfoModel>().Property(m => m.CreateDate)
                .HasColumnType("datetime");
            modelBuilder.Entity<AlbumInfoModel>().Property(m => m.IsPublic).HasColumnType("bool");


            base.OnModelCreating(modelBuilder);
        }

        
    }
}
